import requests
import os
import sys
import json
import datetime
import re
# 解决包导入问题
cwd = os.getcwd()
sys.path.append(cwd)
from pub_func import file_readlines
from proxy import ValidIp
from lxml import etree
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options

from log_handler import LogHandler
from util.config import GetConfig

# 获取文件名
def get_filename(path):
	filename = os.path.basename(path)
	filename = filename.split('.')[0]
	return filename

# 获取当前执行程序的文件名
def get_execute_filename():
	return get_filename(__file__)

log = LogHandler(get_execute_filename())

# 调用接口
def post_caigou_data(self, data):
    host = 'http://192.168.50.75:5001'
    r = requests.post(host + '/api/v1/cg/', data=data)
    return(r.text)

if __name__ == '__main__':
    list_caigou_info = file_readlines('/media/ubuntu/08f29263-3882-43db-96fd-febb6f2e2e5f/spider_data/caigou/20191119/2.csv')
    i = 0
    for caigou_info in list_caigou_info:
        kind, title, date_str, buyer, agent, category, area, desc, url, key = caigou_info.strip().split(',')
        date_at = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
        data = {'kind':kind, 'title':title, 'date_at':date_at, 'buyer':buyer, 'agent':agent, 'category':category, 'area':area, 'desc':desc, 'url':url, 'key':key}
        print(i, data)
        post_caigou_data(data) # 向接口发送数据
        i += 1
